package com.ant.storage.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ant.storage.handler.AntException;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class NullInspectUtil {
    /**
     * 对象属性空值判断
     * @param obj
     * @param str Null判定忽略值
     * @return
     * @throws IllegalAccessException
     */
    public static String ClassnullInspect(Object obj,String ...str){
        return MapNullInspect(ClassChangeMap(obj),str);
    }

    /**
     * 对象转变为Map工具  保留Null值
     * @param obj
     * @return
     */
    public static Map ClassChangeMap(Object obj){
//       return (Map)JSONObject.parse(JSON.toJSONString(obj,SerializerFeature.WriteMapNullValue));
        return JSONObject.parseObject(JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue),Map.class);
    }

    /**
     * 对象转变为map  不保留Null值(最多支持三层对象嵌套)
     * @param obj
     * @return
     */
    public static Map ClassChangeMapNotNull(Object obj){
        Map map = JSON.parseObject(JSON.toJSONString(obj));
        Map mapO = new HashMap();
        Iterator<Map.Entry<Object, Object>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<Object, Object> entry = it.next();
            if(entry.getValue().equals("")){
                it.remove();//使用迭代器的remove()方法删除元素
            }else{
                try{
                    Map map01 = JSON.parseObject(String.valueOf(entry.getValue()));
                    Iterator<Map.Entry<Object, Object>> it01 = map01.entrySet().iterator();
                    while(it01.hasNext()){
                        Map.Entry<Object, Object> entry01 = it01.next();
                        if(entry01.getValue().equals("")){
                            it01.remove();//使用迭代器的remove()方法删除元素
                        }else{
                            try{
                                Map map02 = JSON.parseObject(String.valueOf(entry01.getValue()));
                                Iterator<Map.Entry<Object, Object>> it02 = map02.entrySet().iterator();
                                while(it02.hasNext()) {
                                    Map.Entry<Object, Object> entry02 = it02.next();
                                    if (entry02.getValue().equals("")) {
                                        it02.remove();//使用迭代器的remove()方法删除元素
                                    }else {
                                        mapO.put(entry02.getKey(),entry02.getValue());
                                    }
                                }
                            }catch (ClassCastException ignored){
                                mapO.put(entry01.getKey(),entry01.getValue());
                            }
                        }
                    }
                }catch (ClassCastException ignored){
                    mapO.put(entry.getKey(),entry.getValue());
                }
            }
        }
        return mapO;
    }
    /**
     * Map去除Null值工具
     * @param map
     * @return
     */
    public static Map MapOutNull(Map map){
        for (Object obj:map.keySet()) {
            if(map.get(obj) == null || map.get(obj).equals("")){
                map.remove(obj);
            }
        }
        return map;
    }

    /**
     * Map
     * @param map
     * @param str  Null判定忽略值以及对象里面包含的其他对象
     * @return
     */
    public static String MapNullInspect(Map map, String ... str){
        Boolean bool = false;
        for (Object obj:map.keySet()) {
            for (String aStr : str) {
                if (obj.equals(aStr)) {
                    bool = true;
                    break;
                }
            }
            if (!bool){
                if (map.get(obj) == null || map.get(obj).equals("")) {
                    return obj.toString();
                }
            }
            bool = false;
        }
        return null;
    }
    
    public static void myAnnotationError(Object object, String str) throws NoSuchFieldException {
        Class<?> clazz = object.getClass();
        Field field = clazz.getDeclaredField(str);// 暴力获取private修饰的成员变量
        ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
        throw new AntException(20001,apiModelProperty.value()+"为空");
    }
}
